<?php

/**
 * 小阶段服务数据管理
 */
namespace Beerus\Modules\DesignerService\Controllers;

use App\Http\Controllers\Controller;
use Doraemon\model\DesignerService\DesignerServiceContent;
use Doraemon\service\DesignerService\AppletDesignerService;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use App\Pockets\GridDataModel as pGridDataModel;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\MessageBag;
use Encore\Admin\Widgets\Form;

class StageServiceController extends Controller
{
	const PAGE_SIZE = 20;

	const STAGE = [
		1 => '设计准备',
		2 => '初稿设计',
		3 => '深化设计',
		4 => '设计落地&施工'
	];

	public function __construct()
	{
		parent::__construct();
		Admin::script('$(".pjax-container").attr("id","pjax-container");');
	}

	public function index(Request $request, Content $content)
	{
		$request_data = $request->all();

		$page_size = $request->input('per_page', self::PAGE_SIZE);
		$page = $request->input('page', 1);

		$condition = [];
		if (!empty($request_data['stage_id'])) {
			$condition['stage_id'] = $request_data['stage_id'];
		}

		$condition['LIMIT'] = [
			($page - 1) * $page_size,
			$page_size
		];

		$lists = DesignerServiceContent::select('*', $condition);

		$total = 0;
		if (!empty($lists)) {
			unset($condition['ORDER']);
			unset($condition['LIMIT']);
			$total = DesignerServiceContent::count($condition);
		}

		$gridModel = new pGridDataModel($lists, $total, [
			'perPage' => $page_size
		]);
		$gridList = Admin::grid(
			$gridModel,
			function (Grid $grid) use ($gridModel) {
				$grid->paginate($gridModel->getPerPage());
				$grid->disableExport();
				$grid->disableRowSelector();

				$grid->filter(function (Grid\Filter $filter) {
					$filter->disableIdFilter();
					$filter->column(1 / 2, function ($filter) {
						$filter->equal('stage_id', '服务阶段')->select(self::STAGE);
					});
					$filter->expand();
				});

				$grid->column('id', 'ID');
				$grid->column('stage_id', '服务阶段')->display(function () {
					return self::STAGE[$this->stage_id];
				});
				$grid->column('title', '标题');
				$grid->column('desc', '描述');
				$grid->column('icon', 'PC端icon')->display(function () {
					$icon = AppletDesignerService::getPicUrl($this->icon);
					$img = "<img width='30' height='30' src='{$icon}'>";
					return implode('<br/>', [$img]);
				});
				$grid->column('applet_dark_icon', '小程序暗icon')->display(function () {
					$applet_dark_icon = AppletDesignerService::getPicUrl($this->applet_dark_icon);
					$img = "<img width='30' height='30' src='{$applet_dark_icon}'>";
					return implode('<br/>', [$img]);
				});
				$grid->column('applet_bright_icon', '小程序亮icon')->display(function () {
					$applet_bright_icon = AppletDesignerService::getPicUrl($this->applet_bright_icon);
					$img = "<img width='30' height='30' src='{$applet_bright_icon}'>";
					return implode('<br/>', [$img]);
				});
				$grid->column('app_icon', 'APP端的icon')->display(function () {
					$app_icon = AppletDesignerService::getPicUrl($this->app_icon);
					$img = "<img width='30' height='30' src='{$app_icon}'>";
					return implode('<br/>', [$img]);
				});
				$grid->column('is_required', '是否为必备服务')->display(function () {
					return DesignerServiceContent::REQUIRED[$this->is_required];
				});
				$grid->column('min_days', '最少配置天数');
				$grid->column('default_days', '默认展示天数');
				$grid->column('data_status', '数据状态')->display(function () {
					return DesignerServiceContent::STATUS[$this->data_status];
				});
				$grid->column('add_time', '创建时间')->display(function () {
					return empty($this->add_time) ? '-' : date('Y-m-d H:i:s', $this->add_time);
				});
				$grid->column('edit_time', '修改时间')->display(function () {
					return empty($this->edit_time) ? '-' : date('Y-m-d H:i:s', $this->edit_time);
				});
				$grid->column("admin_id", '操作人')->display(function () {
					if (!$this->admin_id) {
						return '-';
					} else {
						return Admin::user()->find($this->admin_id)->name;
					}
				});

				$grid->actions(function (Grid\Displayers\Actions $actions) {
					$actions->disableDelete();
				});
			}
		);

		$tab = new Tab();
		$tab->add('小阶段服务列表', $gridList->render());
		$content->row($tab);
		return $content
			->header('小阶段服务数据')
			->breadcrumb(['text' => '小阶段服务列表']);
	}

	public function edit(Request $request, Content $content, $id = 0)
	{
		$request = $request->all();

		if ($id == 0) {
			$is_create = true;
			$header = '添加小阶段服务';
		} else {
			$is_create = false;
			$header = '修改小阶段服务';
		}

		$tab = new Tab();
		$tab->add('小阶段服务信息', self::editAction($request, $id, $is_create));
		return $content->header($header)
			->breadcrumb([
				'text' => '小阶段服务列表',
				'url' => route('stage_service.list')
			], ['text' => $header])->row($tab);
	}

	public function editAction($request, $id = 0, $is_create = false)
	{
		if ($is_create) {
			$data = [];
			$form = new Form($data);
		} else {
			$data = DesignerServiceContent::get('*', ['id' => $id]);
			$form = new Form($data);
			$form->hidden('id');
		}
		$form->select('stage_id', '服务阶段')->setWidth(4, 2)
			->options(self::STAGE)
			->required('不能为空');
		$form->text('title', '服务内容标题')->setWidth(4, 2)->required('不能为空');
		$form->textarea('desc', '服务内容描述')->setWidth(4, 2)->required('不能为空');
		$form->select('is_required', '是否为必备服务')->setWidth(4, 2)
			->options(DesignerServiceContent::REQUIRED)->required('不能为空');
		$form->textarea('optional_state', '更多服务解释')->setWidth(4, 2);
		$form->imgUpload('icon', 'PC端使用的icon');
		// https://img.haohaozhu.cn/Op-imageShow/a37a130af08c0Od00qsxx75?iv=1
		$form->imgUpload('applet_dark_icon', '赋能小程序使用的灰色icon');
		$form->imgUpload('applet_bright_icon', '赋能小程序使用的点亮icon');
		$form->imgUpload('app_icon', 'APP端使用的icon');
		$form->number('min_days', '最少配置天数')->default(0)->setWidth(4, 2)->required('不能为空');
		$form->number('default_days', '默认展示天数')->default(0)->setWidth(4, 2)->required('不能为空');
		$form->select('data_status', '数据状态')->default(DesignerServiceContent::STATUS_NORMAL)
			->setWidth(4, 2)->options(DesignerServiceContent::STATUS)->required('不能为空');

		$form->action('/admin/DesignerService/stage_service/save');

		return $form->render();
	}

	public function submit(Request $request)
	{
		$request = $request->all();

		$data = [
			'stage_id' => $request['stage_id'],
			'icon' => $request['icon'],
			'applet_dark_icon' => $request['applet_dark_icon'],
			'applet_bright_icon' => $request['applet_bright_icon'],
			'app_icon' => $request['app_icon'],
			'title' => $request['title'],
			'desc' => $request['desc'],
			'is_required' => $request['is_required'],
			'optional_state' => $request['optional_state'],
			'min_days' => $request['min_days'],
			'default_days' => $request['default_days'],
			'data_status' => $request['data_status'],
			'admin_id' => Admin::user()->id,
		];

		if ((int)$request['min_days'] > (int)$request['default_days']) {
			$error = new MessageBag([
				'title' =>'最少配置天数不能大于默认天数'
			]);

			return back()->with(compact('error'));
		}

		if (isset($request['id'])) {
			$data['edit_time'] = time();
			$rs = DesignerServiceContent::update($data, [
				'id' => $request['id']
			]);
		} else {
			$data['add_time'] = time();
			$rs = DesignerServiceContent::create($data);
		}

		if ($rs) {
			if (isset($request['id'])) {
				$title = '修改成功';
			} else {
				$title = '添加成功';
			}

			$success = new MessageBag([
				'title' => $title
			]);

			return redirect('/admin/DesignerService/stage_service')->with(compact('success'));
		} else {
			if (isset($request['id'])) {
				$title = '修改失败';
			} else {
				$title = '添加失败';
			}

			$error = new MessageBag([
				'title' => $title
			]);

			return back()->with(compact('error'));
		}
	}
}
